//      join.c
//      
//      Copyright 2012 Rafael Asprilla <drakoniss@Asprilla-PC>
//      
//      This program is free software; you can redistribute it and/or modify
//      it under the terms of the GNU General Public License as published by
//      the Free Software Foundation; either version 2 of the License, or
//      (at your option) any later version.
//      
//      This program is distributed in the hope that it will be useful,
//      but WITHOUT ANY WARRANTY; without even the implied warranty of
//      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//      GNU General Public License for more details.
//      
//      You should have received a copy of the GNU General Public License
//      along with this program; if not, write to the Free Software
//      Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
//      MA 02110-1301, USA.
      


#include <stdio.h>
#include <time.h>
#include <string.h>
#include <stdlib.h>

int main(int argc, char **argv)
{
	char log[500], tmp[] = "jointmpXXXXXX";
	time_t curr_time;
	int size[2], i;
	
	if(argc < 5)
	{
		printf("Usage: %s out_mat_file rows cols proc_file[...]\n", argv[0]);
		return 0;
	}
	if((i = mkstemp(tmp)) == -1)
		return -5;
		
	FILE *prc, *out = fdopen(i, "wb");
	if(out == NULL)
	{
		time(&curr_time);
		sprintf(log, "echo \"%s: No se puede crear el archivo de salida %s.\" >> codserver.log", strtok(ctime(&curr_time),"\n"), argv[1]);
		system(log);
		return -1;
	}

	size[0] = atoi(argv[2]);
	size[1] = atoi(argv[3]);
	i = fwrite(size,sizeof(int),2,out);
	
	if (i != 2)
	{
		time(&curr_time);
		sprintf(log, "echo \"%s: Error de lectura de %s.\" >> codserver.log", strtok(ctime(&curr_time),"\n"), argv[i]);
		system(log);
		fclose(out);
		return -4;
	}
	
	fputc('\n', out);
	time(&curr_time);
	sprintf(log, "echo \"%s: Leyendo %d segmentos.\" >> codserver.log", strtok(ctime(&curr_time),"\n"), argc - 4);
	system(log);
	
	i = 4;
	do{
		time(&curr_time);
		sprintf(log, "echo \"%s: Abriendo archivo %s\" >> codserver.log", strtok(ctime(&curr_time),"\n"), argv[i]);
		system(log);
		
		sprintf(log, "test -f %s", argv[i]);
		while(system(log));
		
		prc = fopen(argv[i], "rb");
		
		if(prc == NULL)
		{
			time(&curr_time);
			sprintf(log, "echo \"%s: No se puede leer el archivo %s.\" >> codserver.log", strtok(ctime(&curr_time),"\n"), argv[i]);
			system(log);
			return -5;
		}
		
		fseek(prc, 0, SEEK_END);
		long s = ftell(prc);
		if(s == -1L)
		{
			time(&curr_time);
			sprintf(log, "echo \"%s: No se puede determinar el tamaño de %s.\" >> codserver.log", strtok(ctime(&curr_time),"\n"), argv[i]);
			system(log);
			fclose(prc);
			fclose(out);
			return -2;
		}
		rewind(prc);
		char buffer[s];
		size_t r, w;
		r = fread(buffer, sizeof(char), s, prc);
		if(r != s)
		{
			time(&curr_time);
			sprintf(log, "echo \"%s: Error de lectura de %s.\" >> codserver.log", strtok(ctime(&curr_time),"\n"), argv[i]);
			system(log);
			fclose(prc);
			fclose(out);
			return -3;
		}	
		w = fwrite(buffer, sizeof(char), s, out);
		if(w != s)
		{
			time(&curr_time);
			sprintf(log, "echo \"%s: Error de escritura en %s.\" >> codserver.log", strtok(ctime(&curr_time),"\n"), argv[1]);
			system(log);
			fclose(prc);
			fclose(out);
			return -4;
		}
		fclose(prc);
		i++;
	}while(i < argc);
	
	fclose(out);
	rename(tmp, argv[1]);
	time(&curr_time);
	sprintf(log, "echo \"%s: Archivo %s creado con éxito.\" >> codserver.log", strtok(ctime(&curr_time),"\n"), argv[1]);
	system(log);
	return 0;
}

